package cn.shop.cloud.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: work
 * @ClassName MyRedisTemplate
 * @description:
 * @author: 李尧坤
 * @create: 2025−01-08 11:04
 * @Version 1.0
 **/

public class MyRedisTemplate {
    //使用连接池
    private static JedisPool jedisPool;

    static {
        //来个配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //设置信息
        config.setMaxIdle(500);
        config.setMaxTotal(500);
        config.setMinIdle(100);

        // public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout)
        jedisPool= new JedisPool(config,"localhost",6379,10000000);
    }

    public <T> void set(KeyPrefix keyPrefix,String key,T data){
   /*     //得到 redis连接对象
        try(Jedis jedis=jedisPool.getResource();){
            //获取到真正的key
           String realKey= keyPrefix.getPrefix()+key;
           if(keyPrefix.getExpireSeconds()>0){
               //延时
               jedis.setex(realKey, keyPrefix.getExpireSeconds(), JSON.toJSONString(data));
           }else{
               //新的数据
               jedis.set(realKey,JSON.toJSONString(data));
           }

        }catch (Exception ex){
               throw new RuntimeException(ex.getMessage());
        }*/


        //得到 redis连接对象
        Jedis jedis=null;

        try{
            jedis= jedisPool.getResource();
            //获取到真正的key
            String realKey= keyPrefix.getPrefix()+key;
            if(keyPrefix.getExpireSeconds()>0){
                //延时
                jedis.setex(realKey, keyPrefix.getExpireSeconds(), JSON.toJSONString(data));
            }else{
                //新的数据
                jedis.set(realKey,JSON.toJSONString(data));
            }

        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            jedis.close();
        }
    }



    public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){
        //得到 redis连接对象
        Jedis jedis=null;

        try{
            jedis= jedisPool.getResource();
            //获取到真正的key
            String realKey= keyPrefix.getPrefix()+key;

            String s = jedis.get(realKey);

            if(StringUtils.isEmpty(s)){
                //空的
                return  null;
            }
            return JSON.parseObject(s,clazz);

            //  set key value   get key ---> value

        }catch (Exception ex){
            ex.printStackTrace();
            // throw  new RuntimeException(ex.getMessage());
        }finally {
            jedis.close();
        }

        return null;
    }


    /**
     * 判断数据是否存在
     * @param keyPrefix
     * @param key
     * @return
     */
    public boolean exists(KeyPrefix keyPrefix,String key){
        try(Jedis jedis = jedisPool.getResource();){
            String realKey= keyPrefix.getPrefix()+key;
            return  jedis.exists(realKey);
        }catch (Exception ex){
            ex.printStackTrace();
            throw  new RuntimeException(ex.getMessage());
        }

        // return null;
    }


    public <T> void hset(KeyPrefix keyPrefix,String key,String field,T data){

        try(Jedis jedis = jedisPool.getResource();){
            String realKey= keyPrefix.getPrefix()+key;
            //数据存储
            jedis.hset(realKey,field,JSON.toJSONString(data));

        }catch (Exception ex){
            ex.printStackTrace();
            throw  new RuntimeException(ex.getMessage());
        }

    }


    public <T> T hget(KeyPrefix keyPrefix,String key,String field,Class<T> clazz){

        try(Jedis jedis = jedisPool.getResource();){
            String realKey= keyPrefix.getPrefix()+key;
            //数据存储
            String obj = jedis.hget(realKey, field);

            if(obj==null){
                return  null;
            }
            //拿到数据
            return   JSON.parseObject(obj,clazz);


        }catch (Exception ex){
            ex.printStackTrace();
            throw  new RuntimeException(ex.getMessage());
        }

    }


    public <T> Map<String,T> hgetAll(KeyPrefix keyPrefix, String key, Class<T> clazz){

        try(Jedis jedis = jedisPool.getResource();){
            String realKey= keyPrefix.getPrefix()+key;
            //数据存储

            Map<String, String> map = jedis.hgetAll(realKey);

            if(map==null){
                return  null;
            }

            Map<String,T> resultMap = new HashMap<>();

            for(Map.Entry<String,String> entry:map.entrySet() ){
                resultMap.put(entry.getKey(),JSON.parseObject(entry.getValue(),clazz));
            }

            return  resultMap;
        }catch (Exception ex){
            ex.printStackTrace();
            throw  new RuntimeException(ex.getMessage());
        }

    }

    public Long incr(KeyPrefix keyPrefix, String key){
        try(Jedis jedis = jedisPool.getResource();){
            String realKey= keyPrefix.getPrefix()+key;

            return jedis.incr(realKey);
        }catch (Exception ex){
            ex.printStackTrace();
            throw  new RuntimeException(ex.getMessage());
        }
    }

    public Long decr(KeyPrefix keyPrefix, String key){
        try(Jedis jedis = jedisPool.getResource();){
            String realKey= keyPrefix.getPrefix()+key;

            return jedis.decr(realKey);
        }catch (Exception ex){
            ex.printStackTrace();
            throw  new RuntimeException(ex.getMessage());
        }
    }


}